#ifndef __SDMNG_H__
#define __SDMNG_H__

#include <stdio.h>
#include <sys/stat.h>

/*--------------------------------------------------------------------------*/
//	Data Structures Definition
/*--------------------------------------------------------------------------*/
#define SD_MNT_DIR "/mnt/sd1"
#define SD_DEV "/dev/mmcblk0p1"

#define MAX_SD_DEV_NUM 7

#define DEV_SD_TYPE 	0x0100 
#define DEV_HD_TYPE 	0x0200 
#define DEV_SUB_SDIO 	0x0001
#define DEV_SUB_USB	0x0002

typedef enum _sd_status 
{
    NO_EXIST = 0,   
    EXIST_NOMOUNT,
    EXIST_READ_ONLY,
    EXIST_RDWR_OK, 
    EXIST_LESS_SPC,
    EXIST_ILLEGAL 
} sd_status_t;

enum sd_fs_type
{
    FS_VFAT = 1,
    FS_EXT3,
    FS_MAX
};

typedef struct storage_dev_meta
{
    int dev_type;// type sd = 1
    int dev_subtype;// To SD DEV,may usb type or sdio type.
    int fs_type; // filesystem type: vfat, ext3
    int max_num; /* system max device number supported */
    int prev_devid; /* last recently used device ID */
    char mpoint[64]; /* absolute root mount point: e.x./mnt  */
    /* filter content,return TRUE to filter-in or FALSE to filter-out */
    int (*filter)(const char *name,struct stat*ststat);
    /* callback to delete content,return 0 if delete success,or -1 if fail */
    int (*delter)(const char *name,struct stat*ststat);
}st_rsc_dev_meta;

struct sd_info 
{
    char    devname[64];//sd device node name in /dev
    char    mpoint[64]; //sd mount point name
    unsigned int    dev_id;	    // 0 - maxnum-1
    unsigned long long  freespace;  //unit MB
    unsigned long long  totalspace; //unit MB
    sd_status_t status;
};

#define TYPE_SD_FILE 1
#define TYPE_SD_DIR  2
struct file_item_info 
{
    int type; 
    char fname[64]; 
    unsigned int fsize; //unit: byte,max to 4GB
};
struct sd_item_list
{
    int cnt;
    struct file_item_info item[];
};


/*--------------------------------------------------------------------------*/
//  Functions Declerations
/*--------------------------------------------------------------------------*/
/**** sd card action control *********/
int rsc_mount_device(int devid);
int rsc_umount_device(int devid);
int rsc_format_device(int devid);
int rsc_mount_all(void);
int rsc_umount_all(void);

#define RSC_FORMAT_FLAG 1
#define RSC_FCHECK_FLAG 2
int rsc_get_dev_busy(int devid);
int rsc_set_dev_busy(int devid,int flag);

/**
 * only should be called once in rsc_process_init 
 */
int rsc_set_prev_devid(int devid);

/**** sd status and info access ******/
/*
 * get available device to record
 * @param[in] size the size need to use,unit is MB
 * @param[in,out] mpoint the device mount point
 * @ret if fail,return -1;if success,return 0
 */
int rsc_get_avail_dev(int size, char *mpoint);
/**
 * get sd device information and status
 * @param[in] devid device id from 0 - maxnum-1
 * @param[in,out] info store the information and status
 * @ret if failed return -1, only info->status is valid
 * if success return 0, all info is valid.
 */
int rsc_get_sd_info(int devid, struct sd_info *info);

/**
 * get sd [id] mount point,such as : /mnt/sd1
 */
int rsc_get_sd_mpoint(int devid, char *mpoint,int len);
int rsc_get_sd_devid(const char *mpoint);

/************ sd content get and manage *******************/
/**
 * rsc_get_file_list -- get sd card path file list
 *
 * @path[in] -- the path in sd card,relative to sd mount point,NULL for mpoint-self.
 * @list[out] -- the file list get from path,malloced by api.
 * @ret if file list size > 0 return 0,else return -1.
 * @note when return value is 0,CALLER should free *list after used it!!!
 */
int rsc_get_file_list(const char *path,struct sd_item_list **list);

/**
 * rsc_del_file -- delete sd card path file 
 *
 * @path[in] -- the path in sd card,relative to sd mount point,NULL for mpoint-self.
 */
int rsc_del_file(const char *path);

int rsc_set_loop_delete(int devid,int size);


/**** RSC storage init ,process,exit ******/
int rsc_init_storage(st_rsc_dev_meta *dmeta);
void *rsc_storage_process(void *arg);
void *rsc_fix_dev_process(void *arg);
int rsc_exit_storage(void);

#endif

